import '../model/chat_group_model.dart';

/// 群聊默认数据
/// 提供6条默认的群聊数据
class ChatGroupData {
  /// 获取所有默认群聊数据
  static List<ChatGroupModel> getDefaultGroups() {
    final now = DateTime.now();

    return [
      // Group 1 - Green Plant Care Exchange Group
      ChatGroupModel(
        id: '1',
        title: 'Green Plant Care Exchange Group',
        description:
            'Share daily care tips for green plants—from watering frequency to light adjustment—and solve problems like yellow leaves together.',
        coverImage: 'assets/images/group1.png',
        creatorId: '1', // admin
        createTime: now.subtract(const Duration(days: 10)),
        memberIds: ['1', '2', '3', '4', '5'],
      ),

      // Group 2 - Succulent Lovers Gathering
      ChatGroupModel(
        id: '2',
        title: 'Succulent Lovers Gathering',
        description:
            'A hub for succulent fans! Talk about pot selection, propagation tricks, and show off your "fat little plants"\' growth status.',
        coverImage: 'assets/images/group2.png',
        creatorId: '2', // gardenmaster
        createTime: now.subtract(const Duration(days: 8)),
        memberIds: ['2', '3', '4', '6', '7', '8'],
      ),

      // Group 3 - Balcony Flower Planting Class
      ChatGroupModel(
        id: '3',
        title: 'Balcony Flower Planting Class',
        description:
            'Learn how to turn your balcony into a small flower garden—discuss suitable flower varieties, soil matching, and seasonal maintenance.',
        coverImage: 'assets/images/group3.png',
        creatorId: '3', // leafyfriend
        createTime: now.subtract(const Duration(days: 6)),
        memberIds: ['3', '1', '5', '7'],
      ),

      // Group 4 - Gardening Beginners Mutual Aid Group
      ChatGroupModel(
        id: '4',
        title: 'Gardening Beginners Mutual Aid Group',
        description:
            'New to gardening? Ask questions about basic tools, plant selection, and common issues here—everyone helps each other grow.',
        coverImage: 'assets/images/group4.png',
        creatorId: '4', // plantcare
        createTime: now.subtract(const Duration(days: 4)),
        memberIds: ['4', '1', '2', '6', '8'],
      ),

      // Group 5 - Indoor Green Plant Growth Camp
      ChatGroupModel(
        id: '5',
        title: 'Indoor Green Plant Growth Camp',
        description:
            'Focus on indoor plants! Share how to keep pothos, monsteras, and peace lilies thriving in low-light or air-conditioned rooms.',
        coverImage: 'assets/images/group5.png',
        creatorId: '5', // greensoul
        createTime: now.subtract(const Duration(days: 2)),
        memberIds: ['5', '1', '3', '7'],
      ),

      // Group 6 - Flower Growing Experience Sharing Station
      ChatGroupModel(
        id: '6',
        title: 'Flower Growing Experience Sharing Station',
        description:
            'Exchange practical experiences: how to make flowers bloom longer, prevent pests, and reuse old materials for planting.',
        coverImage: 'assets/images/group6.png',
        creatorId: '6', // blooming
        createTime: now.subtract(const Duration(hours: 12)),
        memberIds: ['6', '2', '4', '8'],
      ),
    ];
  }

  /// 根据ID获取群聊
  static ChatGroupModel? getGroupById(String id) {
    try {
      return getDefaultGroups().firstWhere((group) => group.id == id);
    } catch (e) {
      return null;
    }
  }

  /// 根据创建者ID获取群聊
  static List<ChatGroupModel> getGroupsByCreatorId(String creatorId) {
    return getDefaultGroups()
        .where((group) => group.creatorId == creatorId)
        .toList()
      ..sort((a, b) => b.createTime.compareTo(a.createTime));
  }

  /// 根据成员ID获取群聊
  static List<ChatGroupModel> getGroupsByMemberId(String memberId) {
    return getDefaultGroups()
        .where((group) => group.memberIds.contains(memberId))
        .toList()
      ..sort((a, b) => b.createTime.compareTo(a.createTime));
  }

  /// 获取群聊统计信息
  static Map<String, int> getGroupStatistics() {
    final groups = getDefaultGroups();
    final stats = <String, int>{};

    // 总群聊数
    stats['totalGroups'] = groups.length;

    // 总成员数
    stats['totalMembers'] = groups.fold(
      0,
      (sum, group) => sum + group.memberCount,
    );

    // 平均成员数
    stats['averageMembers'] =
        groups.isNotEmpty
            ? (stats['totalMembers']! / groups.length).round()
            : 0;

    return stats;
  }

  /// 获取最受欢迎的群聊（按成员数排序）
  static List<ChatGroupModel> getMostPopularGroups() {
    final groups = getDefaultGroups();
    groups.sort((a, b) => b.memberCount.compareTo(a.memberCount));
    return groups;
  }

  /// 获取最新群聊
  static List<ChatGroupModel> getLatestGroups() {
    final groups = getDefaultGroups();
    groups.sort((a, b) => b.createTime.compareTo(a.createTime));
    return groups;
  }
}
